package com.bd.demo.ui

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.core.tween
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.foundation.layout.*
import androidx.compose.material.Snackbar
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import com.bd.demo.model.AppViewModel
import com.bd.demo.model.BleViewModel
import com.bd.demo.ui.page.BLE_LIST_SCREEN
import com.bd.demo.ui.page.BleListScreen
import com.bd.demo.ui.page.MESSAGE_SCREEN
import com.bd.demo.ui.page.MessageScreen
import com.bd.demo.ui.theme.Bd_demoTheme
import com.bd.demo.ui.theme.lightSecondary
import com.bd.demo.ui.theme.text14MediumOnSecondary
import com.bd.demo.ui.view.LoadingDialog
import com.bd.demo.ui.view.NormalDialog
import com.google.accompanist.insets.navigationBarsWithImePadding
import com.google.accompanist.navigation.animation.AnimatedNavHost
import com.google.accompanist.navigation.animation.composable
import com.google.accompanist.navigation.animation.rememberAnimatedNavController

class MainActivity : BaseActivity() {

    private val app: AppViewModel by viewModels()

    @ExperimentalAnimationApi
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Bd_demoTheme {
                val navController = rememberAnimatedNavController()

                // 提示
                val isShowSnackBar by app.isShowSnackBar.observeAsState(false)
                val snackBarValue by app.snackBarValue.observeAsState("")

                Box(
                    modifier = Modifier
                        .fillMaxSize(),
                    contentAlignment = Alignment.BottomCenter
                ) {
                    MainNavigation(navController, app)
                    if (isShowSnackBar) {
                        Snackbar(
                            modifier = Modifier
                                .fillMaxWidth()
                                .wrapContentHeight()
                                .padding(16.dp)
                                .navigationBarsWithImePadding(),
                            backgroundColor = lightSecondary
                        ) {
                            Text(text = snackBarValue, style = text14MediumOnSecondary)
                        }
                    }

                    // 加载中弹窗
                    val isShowLoading by app.isShowLoading.observeAsState(false)
                    LoadingDialog(isShow = isShowLoading) {
                        app.cancelLoading()
                    }

                    // 普通弹窗
                    val isShowNormalDialog by app.isShowNormalDialog.observeAsState(false)
                    val normalDialogBuilder by app.normalDialogBuilder.observeAsState(null)
                    NormalDialog(isShow = isShowNormalDialog, builder = normalDialogBuilder) {
                        app.dismissNormalDialog()
                    }
                }
            }
        }
    }


}


/**
 * 页面导航
 */
@ExperimentalAnimationApi
@Composable
fun MainNavigation(
    navController: NavHostController,
    app: AppViewModel
) {
    val bleViewModel: BleViewModel = viewModel()

    AnimatedNavHost(
        navController,
        BLE_LIST_SCREEN,
        modifier = Modifier
            .fillMaxSize(),
        enterTransition = { _,_ ->
            slideInHorizontally( { it } , tween(300) )
        },
        exitTransition = { _,_ ->
            slideOutHorizontally( { -it } , tween(300) )
        },
        popEnterTransition = { _, _ ->
            slideInHorizontally( { -it } , tween(300) )
        },
        popExitTransition = { _, _ ->
            slideOutHorizontally( { it } , tween(300) )
        }
    ) {
        // 蓝牙设备列表页
        composable(BLE_LIST_SCREEN) {
            BleListScreen(navController, app, bleViewModel)
        }
        // 消息页面
        composable(MESSAGE_SCREEN) {
            MessageScreen(navController, app, bleViewModel)
        }
    }
}